package net.hanjava.test;

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Util {
	private Util() {}
	public static void test(Component comp) {
		if(comp instanceof JComponent) {
			JComponent jcomp = (JComponent) comp;
			JFrame frame = new JFrame(jcomp.toString());
			frame.setContentPane( jcomp );
			frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
			frame.pack();
			frame.setLocationByPlatform(true);
			frame.setVisible( true );
		} else {
			Frame frame = new Frame(comp.toString());
			frame.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			});
			frame.add( comp );
			frame.pack();
			frame.setVisible( true );
		}
	}

	public static long getUsedMemory() {
		Runtime rt = Runtime.getRuntime();
		return rt.totalMemory() - rt.freeMemory();
	}

	public static void pause() {
		byte[] buf = new byte[100];
		System.out.println("Press enter to continue");
		try {
			System.in.read(buf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}